#include<allegro.h>
#include<stdlib.h>
#include"fonction.h"
#include"intel.h"
#include"scene.h"

/*saisie de tout les image du jeu pour les deux foction suivantes*/
void sasirguru(guru *gur1){     gur1->img_right[0]=load_bitmap("../img/enemies/0right.bmp",NULL);
				gur1->img_right[1]=load_bitmap("../img/enemies/1right.bmp",NULL);
				gur1->img_right[2]=load_bitmap("../img/enemies/2right.bmp",NULL);
				gur1->img_right[3]=load_bitmap("../img/enemies/3right.bmp",NULL);
				gur1->img_right[4]=load_bitmap("../img/enemies/4right.bmp",NULL);
			  	gur1->img_left[0]=load_bitmap("../img/enemies/0left.bmp",NULL);
				gur1->img_left[1]=load_bitmap("../img/enemies/1left.bmp",NULL);
				gur1->img_left[2]=load_bitmap("../img/enemies/2left.bmp",NULL);
				gur1->img_left[3]=load_bitmap("../img/enemies/3left.bmp",NULL);
				gur1->img_left[4]=load_bitmap("../img/enemies/4left.bmp",NULL);
			  	
              (*gur1).blood=2;
			

			  	 (*gur1).x=(SCREEN_W/2)+600 ;
               
				 (*gur1).y=(SCREEN_H/2)-220;

				 gur1->marteau[0]=load_bitmap("../img/enemies/mart/1.bmp",NULL);
				gur1->marteau[1]=load_bitmap("../img/enemies/mart/2.bmp",NULL);
				gur1->marteau[2]=load_bitmap("../img/enemies/mart/3.bmp",NULL);
				gur1->marteau[3]=load_bitmap("../img/enemies/mart/4.bmp",NULL);
				}

void saisirhero (heros *hero) {
					hero->img_right=load_bitmap("../img/hero/2.bmp",NULL);
					hero->img_left=load_bitmap("../img/hero/1.bmp",NULL);
					hero->attack_sabrel[0]=load_bitmap("../img/hero/attleft0.bmp",NULL); 
					hero->attack_sabrel[1]=load_bitmap("../img/hero/attleft1.bmp",NULL);
					hero->attack_sabrel[2]=load_bitmap("../img/hero/attleft2.bmp",NULL);
					hero->attack_sabrer[0]=load_bitmap("../img/hero/attright1.bmp",NULL);
					hero->attack_sabrer[1]=load_bitmap("../img/hero/attright2.bmp",NULL);
					hero->attack_sabrer[2]=load_bitmap("../img/hero/attright3.bmp",NULL);
					hero->attack_martr[0]=load_bitmap("../img/hero/sabr1.bmp",NULL);
					hero->attack_martr[1]=load_bitmap("../img/hero/sabr2.bmp",NULL);
					hero->attack_martr[2]=load_bitmap("../img/hero/sabr3.bmp",NULL);
					hero->attack_martl[0]=load_bitmap("../img/hero/sabl1.bmp",NULL);
					hero->attack_martl[1]=load_bitmap("../img/hero/sabl2.bmp",NULL);
					hero->attack_martl[3]=load_bitmap("../img/hero/sabl3.bmp",NULL);
					  
					  (*hero).score=0;
					  (*hero).blood=6;
					hero->damm[0]=load_bitmap("../img/jauge/1.bmp",NULL);
					hero->damm[1]=load_bitmap("../img/jauge/2.bmp",NULL);
					hero->damm[2]=load_bitmap("../img/jauge/3.bmp",NULL);
					hero->damm[3]=load_bitmap("../img/jauge/4.bmp",NULL);
					hero->damm[4]=load_bitmap("../img/jauge/5.bmp",NULL);
					hero->damm[5]=load_bitmap("../img/jauge/6.bmp",NULL);
}

/*faire deplacer le personage a droite*/
void translaterdroit (heros *hero,guru *gur1) {
				
			
			image=hero->img_right;
			blit(background,buffer,0,0,0,0,background->w,background->h);
		 	draw_sprite(buffer,hero->img_right,(*hero).x,(*hero).y); 
			draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
			draw_sprite(buffer,image4,image4->w,image4->h);
		 	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 	(*hero).x += 1;}


/*faire deplacer le personage a gauche*/
void transletergauche(heros *hero,guru *gur1){
		 
		
		
		 image=hero->img_left;
		(*hero).x -= 1;
		 blit(background,buffer,0,0,0,0,background->w,background->h);
		 draw_sprite(buffer,hero->img_left,(*hero).x,(*hero).y);
		 draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
		draw_sprite(buffer,image4,image4->w,image4->h);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 }



/*pour faire sauter le personnage*/
void sauter(heros *hero,guru *gur1){

				for(i=0;i<18;i++){
		(*hero).y -= i;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,(*hero).x,(*hero).y);
		draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
		draw_sprite(buffer,image4,image4->w,image4->h);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 
 
        attendre(0.015);
    
		}
		for(i=0;i<18;i++){
		(*hero).y += i;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,(*hero).x,(*hero).y);
		draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
		draw_sprite(buffer,image4,image4->w,image4->h);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		
 
        attendre(0.015);
    
		}i=0;}

/*sauter avec un deplacement latérale*/
void sursauter(heros *hero,guru *gur1){
		

			if(image==hero->img_right){
			for(i=0;i<18;i++){
			j=j+1;
			(*hero).y -= i;
			(*hero).x += j/3;
			blit(background,buffer,0,0,0,0,background->w,background->h);
			draw_sprite(buffer,image,(*hero).x,(*hero).y);
			draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
			draw_sprite(buffer,image4,image4->w,image4->h);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
			
	 
		attendre(0.015);
	    
			}
			for(i=0;i<18;i++){
			(*hero).y += i;
			(*hero).x += j/3;
			blit(background,buffer,0,0,0,0,background->w,background->h);
			draw_sprite(buffer,image,(*hero).x,(*hero).y);
			draw_sprite(buffer, image2,(*gur1).x,(*gur1).y);
			draw_sprite(buffer,image4,image4->w,image4->h);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
			
	 
		attendre(0.015);
	    
			}i=0;
			 j=0;}
			 
			 
			 
			 
			 
			 else{
			 
			for(i=0;i<18;i++){
			j=j+1;
			(*hero).y -= i;
			(*hero).x -= j/3;
			blit(background,buffer,0,0,0,0,background->w,background->h);
			draw_sprite(buffer,image,(*hero).x,(*hero).y);
			draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
			draw_sprite(buffer,image4,image4->w,image4->h);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
			
	 
		attendre(0.015);
	    
			}
			for(i=0;i<18;i++){
			(*hero).y += i;
			(*hero).x -= j/3;
			blit(background,buffer,0,0,0,0,background->w,background->h);
			draw_sprite(buffer,image,(*hero).x,(*hero).y);
			draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
			draw_sprite(buffer,image4,image4->w,image4->h);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
			
	 
		attendre(0.015);
	    
			}i=0;
			 j=0;}
		 }




void attendre(float temps)
{
    clock_t arrivee=clock()+(temps*CLOCKS_PER_SEC); 
 
    while(clock()<arrivee);
}



/** 
*cette fonction fait une attaque pour l'enemies*/
int lancermarteau(heros *hero,guru *gur1){ m=(*gur1).x;l=0;while (m>0 && m<SCREEN_W)

						{
							if (image2==gur1->img_left[0]){if(l>3)l=0;
								image3=gur1->marteau[l];
								m=m-2;
								l++;
								blit(background,buffer,0,0,0,0,background->w,background->h);
								draw_sprite(buffer,image,(*hero).x,(*hero).y);
								draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
								draw_sprite(buffer,image3,m,(*gur1).y+80);
								draw_sprite(buffer,image4,image4->w,image4->h);
								blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
								if (key[KEY_A])sursautermart(hero,gur1);
								if (key[KEY_RIGHT] && (*hero).x < SCREEN_W -50)translaterdroitmart (hero,gur1);
								if (key[KEY_LEFT] && (*hero).x > 0)transletergauchemart(hero,gur1);
								if(key[KEY_Z]){
								attaquersabremart(hero,gur1,&nbre);}
								if(m<((*hero).x+70) && m<((*hero).x+60)) {
									b++ ;
									image4=hero->damm[b];return 0;}
										
								
	 
													attendre(0.005);
		
							}

							if (image2==gur1->img_right[0]){if(l>3)l=0;
											image3=gur1->marteau[l];
											m=m+2;l++;
								blit(background,buffer,0,0,0,0,background->w,background->h);
								draw_sprite(buffer,image,(*hero).x,(*hero).y);
								draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
								draw_sprite(buffer,image3,m,(*gur1).y+80);
								draw_sprite(buffer,image4,image4->w,image4->h);
								blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
								if (key[KEY_A])sursautermartgau(hero,gur1);
								if (key[KEY_RIGHT] && (*hero).x < SCREEN_W -50)translaterdroitmart (hero,gur1);
								if (key[KEY_LEFT] && (*hero).x > 0)transletergauchemart(hero,gur1);
								if(m<((*hero).x+70) && m<((*hero).x+60)) {b++;
											image4=hero->damm[b];return 0;}
								
	 
													attendre(0.005);
		
							}
}}

/** la fonction attaquer avec le sabre*/
void attaquersabre(heros *hero,guru *gur1,int *nbre)
{	
	for(i=0;i<3;i++)
	{
		if(image==hero->img_right){mag=hero->attack_sabrer[i];
					blit(background,buffer,0,0,0,0,background->w,background->h);
					draw_sprite(buffer,mag,(*hero).x,(*hero).y);
					draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
					draw_sprite(buffer,image4,image4->w,image4->h);
					blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);}

		if(image==hero->img_left){mag=hero->attack_sabrel[i];
					blit(background,buffer,0,0,0,0,background->w,background->h);
					draw_sprite(buffer,mag,((*hero).x)-50,(*hero).y);
					draw_sprite(buffer,image2,(*gur1).x,(*gur1).y);
					draw_sprite(buffer,image4,image4->w,image4->h);
					blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);}
	
	attendre(0.01);
	}
	if((*hero).x<((*gur1).x+50) && ((*hero).x>((*gur1).x))){*nbre=*nbre-1;}
}





















